/**   @desciption 判断是否有某个class
 *   @params(el) DOM元素
 *   @params(className) 添加的class
 */
export function hasClass (el, className) {
  let reg = new RegExp('(^|\\s)' + className + '(\\s|$)');
  return reg.test(el.className)
}

/**   @desciption 添加class
 *   @params(el) DOM元素
 *   @params(className) 添加的class
 */
export function addClass (el, className) {
  if (hasClass(el, className)) {
    return
  }
  let newClass = el.className.split(' ');
  newClass.push(className);
  el.className = newClass.join(' ')
}

/**
 * @description 删除class
 * @param {el} DOM元素
 * @param {className} 删除的class
 */
export function removeClass(el, className) {
  if (hasClass(el, className)) {
    var reg = new RegExp('(\\s|^)' + className + '(\\s|$)');
    el.className = el.className.replace(reg, ' ');
  }
}

/**   @desciption 获取data-属性
 *   @params(el) DOM元素
 *   @params(name) data-后面的名
 */
export function getData (el, name, val) {
  const prefix = 'data-';
  if (val) {
    return el.setAttribute(prefix + name, val)
  }
  return el.getAttribute(prefix + name)
}

/**  @name 格式化时间
 *  @param timestamp(时间戳)
 *  @param type 1返回年月日，2返回一般格式，否则返回月，日，时分秒
 */
export function stampToDate (timestamp, type) {
  let date = new Date(timestamp);
  let Y = date.getFullYear();
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
  let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  if (type === 1) {
    return Y + '-' + M + '-' + D;
  } else if (type === 2) {
    return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s;
  } else if (type === 3) {
    return h + ':' + m;
  } else {
    return M + '-' + D + ' ' + h + ':' + m + ':' + s;
  }
}

/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
  if (!name) return;
  if (typeof content !== 'string') {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(name, content);
};

/**
 * 获取localStorage
 */
export const getStore = name => {
  if (!name) return;
  return window.localStorage.getItem(name);
};

/**
 * 删除localStorage
 */
export const removeStore = name => {
  if (!name) return;
  window.localStorage.removeItem(name);
};

/**
 * 对象深拷贝
 * @param {Object} obj
 */
export function  copyObj(obj) {
  let res = {}
  for (let key in obj) {
    res[key] = obj[key]
  }
  return res
}

export function getCookie (key) {
  const reg = new RegExp('(^| )' + key + '=([^;]*)(;|$)');
  let cookie;
  return document.cookie.length > 0 && (cookie = document.cookie.match(reg))
    ? unescape(cookie[2])
    : null
}

/**
 * 设置 cookie
 * @param  {String} key
 * @param  {String} value
 * @param  {Number} day 过期时间(单位：天)
 */
export function  setCookie (key, value, day) {
  const exp = new Date();
  exp.setTime(exp.getTime() + day*24*60*60*1000);
  document.cookie = `${key}=${escape(value)};expires=${exp.toGMTString()}`
  // domain=${lallla};
}
export function clearCookie (key) {
  setCookie(key, '', -1)
}

/**
 * 从URL中获取指定参数名称的值
 * @param url
 * @param name
 * @returns {null}
 */
export function getQueryByName(url,name){
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  let r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]); return null;
  }
}
